package com.fang.fen.blog.user.service;

import com.fang.fen.blog.common.id.IdGenerator;
import com.fang.fen.blog.common.security.PdUtil;
import com.fang.fen.blog.user.mapper.UserMapper;
import com.fang.fen.blog.user.util.UserUtil;
import com.fang.fen.blog.user.domain.User;
import com.fang.fen.blog.user.dto.UserDTO;
import com.fang.fen.blog.user.vo.UserVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component
public class UserService {

    @Autowired
    private UserMapper userMapper;


    public int create(UserDTO userDTO) {

        User user = UserUtil.convert(userDTO);
        user.setId(IdGenerator.nextId());
        user.setPassword(PdUtil.encode(user.getPassword()));
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());

        return userMapper.save(user);
    }


    public PageInfo<User> page(int pageIndex, int pageSize, User user) {

        PageHelper.startPage(pageIndex,pageSize);

        return  userMapper.findByPage(user).toPageInfo();
    }
}
